package com.changgou.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.system.mapper.AdminMapper;
import com.changgou.system.pojo.Admin;
import com.changgou.system.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper,Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    /**
     * 增加
     * @param admin
     */
    @Override
    public void add(Admin admin){
        String password = BCrypt.hashpw(admin.getPassword(), BCrypt.gensalt());
        admin.setPassword(password);
        adminMapper.insert(admin);
    }

    /**
     * 登录验证密码
     *
     * @param admin
     * @return
     */
    @Override
    public boolean login(Admin admin) {
        //根据登录名查询管理员
        Admin admin1 = adminMapper.selectOne(Wrappers.<Admin>lambdaQuery().eq(Admin::getLoginName,admin.getLoginName()).eq(Admin::getStatus,"1"));//数据库查询出的对象
        if(ObjectUtil.isNotEmpty(admin1)){
            return false;
        }else{
            //验证密码, Bcrypt为spring的包, 第一个参数为明文密码, 第二个参数为密文密码
            return BCrypt.checkpw(admin.getPassword(),admin1.getPassword());
        }
    }
}
